package com.book.service.impl;

import com.book.mapper.MenuDao;
import com.book.mapper.OperatorRoleDao;
import com.book.mapper.RoleDao;
import com.book.model.Menu;
import com.book.model.Operator;
import com.book.mapper.OperatorDao;
import com.book.model.OperatorRole;
import com.book.model.Role;
import com.book.service.OperatorService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.servlet.http.HttpSession;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author wx
 * @since 2023-10-12 08:17:13
 */
@Service
public class OperatorServiceImpl extends ServiceImpl<OperatorDao, Operator> implements OperatorService {
    @Autowired
    private OperatorDao operatorDao;
    @Autowired
    private MenuDao menuDao;
    @Autowired
    private OperatorRoleDao operatorRoleDao;
    @Autowired
    private RoleDao roleDao;
    @Override
    public List<Operator> getAll() {
        Operator operator = new Operator();
        operator.setOname("qq");
        operatorDao.insert(operator);
        return operatorDao.getAll();
    }

    @Override
    public List<Map<String, Object>> ztreeGetMenuList(HttpSession session) {
        //查询用户权限集合

        Operator operator = (Operator) session.getAttribute("user");
        List<Menu> menus=menuDao.getMenuList(operator.getOid());
        List<Map<String, Object>> ztreeGetMenuList = getZtreeGetMenuList(0, menus);
        return ztreeGetMenuList;
    }



    public List<Map<String,Object>> getZtreeGetMenuList(int operatorid, List<Menu> menus){

        List<Map<String, Object>> list=new ArrayList<>();
        for (Menu menu : menus) {
            if (menu.getOperatorid()==operatorid){
                Map<String,Object> map=new HashMap<>();
                map.put("id",menu.getMid());
                map.put("text",menu.getText());
                map.put("url",menu.getUrl());
                int mid = menu.getMid().intValue();
                map.put("nodes",getZtreeGetMenuList(mid,menus));
                if (getZtreeGetMenuList(mid,menus).size()==0){
                    map.remove("nodes");
                }
                list.add(map);
        }
        }
        return list;
    }

    @Override
    @Transactional
    public void addOperator(Operator operator, Long[] rid) {
       operatorDao.insert(operator);
        Long oid = operator.getOid();
        for (Long r : rid) {
            OperatorRole operatorRole = new OperatorRole();
            operatorRole.setOid(oid);
            operatorRole.setRid(r);
            operatorRoleDao.insert(operatorRole);
        }

    }

    @Override
    @Transactional
    public void updaterOperator(Operator operator, Long[] rid) {
        operatorDao.updateById(operator);
        Long oid = operator.getOid();
        operatorRoleDao.deleteOperatorRole(oid);
        for (Long l : rid) {
            OperatorRole operatorRole = new OperatorRole();
            operatorRole.setOid(oid);
            operatorRole.setRid(l);

        }
    }

    @Override
    @Transactional
    public void deleteOperator(Long oid) {
        operatorRoleDao.deleteOperatorRole(oid);
        operatorDao.deleteById(oid);
    }
}
